﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace dotNetTraining.ASP_NET
{
    public partial class StateManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == true)
            {
                if (ViewState["number"] != null) //Lets retrieve, increase and store again
                {
                    ViewState["number"] = Convert.ToInt32(ViewState["number"]) + 1;
                }
                else //First postback, lets store the info
                {
                    ViewState["number"] = 1;
                }

                lblViewState.Text = ViewState["number"].ToString();
            }

             int postbacks = 0;
            //GetDataItem from querystring
             string temp = Request.QueryString["number"];
            //set in query string          
            if (Request.QueryString["number"] != null) //Lets retrieve, increase and store again
            {
                postbacks = Convert.ToInt32(Request.QueryString["number"]) + 1;
            }
            else //First postback, lets store the info
            {
                postbacks = 1;
            }


            //In web pages
            Application.Lock();
            Application["number"] = Convert.ToInt32(Application["number"]) + 1;
            Application.UnLock();

            lblCooke.Text = Application["number"].ToString();
        
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int result =0;
            try{
                result = int.Parse(TextBox1.Text) + int.Parse(TextBox2.Text);
            }catch{}
               

            Session["Snumber"] = result;
            lblSession.Text = "Session :" + Session["Snumber"].ToString();

            Response.Cookies["Cnumber"].Value = result.ToString();     // save cookies                 
            lblCooke.Text ="Cookies: "+ Response.Cookies["Cnumber"].Value; // get value from cookies

        }

        protected void btnViewState_Click(object sender, EventArgs e)
        {
            //Store in Hidden Field -----------------------------------------------------------
           
            int newVal = Convert.ToInt32(HiddenField_Demo.Value) + 1; //Hidden field default value was 0
            HiddenField_Demo.Value = newVal.ToString();
            lblhdf.Text = HiddenField_Demo.Value;
        }
    }
}